<template>
	<view class="wrap">
		<view class="main" v-if="hasLoad">
			<view class="eat-method">
				<view class="b-title" v-for="(item, index) in deployList" :key="index"
					:class="{'f-active': index === deployIndex}" @click="onChooseDeploy(index)">
					<view class="iconfont" :class="item.icon"></view>
					<view class="s-tit">{{item.name}}</view>
				</view>
			</view>
			<view class="box" v-if="deployList[deployIndex].alias === 'meat'">
				<view class="b-row f-shop">
					<view class="b-tit">桌位号:</view>
					<view class="b-cont f-desk" @click="onScanCode" v-if="deskData">
						<view class="s-text">{{deskData.number}}</view>
					</view>
					<view class="b-cont f-scan" @click="onScanCode" v-else>
						<view class="iconfont icon-scan"></view>
						<view class="s-cont">请扫点餐码</view>
					</view>
				</view>
			</view>
			<view class="box" v-else-if="deployList[deployIndex].alias === 'pack'">
				<view class="b-row f-shop">
					<view class="b-tit">预留电话:</view>
					<view class="b-cont f-phone">
						<input type="text" placeholder="请填写联系电话" @input="onInputText" data-field="contact" />
					</view>
				</view>
			</view>
			<view class="gift-head">{{shopData.name}}</view>
			<view class="gift-list">
				<view class="gift-item" v-for="(item, index) in orderData.goods_list" :key="index">
					<view class="b-left">
						<image :src="item.goods_cover" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title">{{item.goods_name}}</view>
						<view class="b-spec">
							<view class="s-value">{{item.goods_names}}</view>
						</view>
						<view class="b-fund">
							<view class="s-count">x{{item.goods_count}}</view>
							<view class="s-worth">¥{{item.goods_price}}</view>
							<view class="s-num">¥{{item.payment}}</view>
						</view>
					</view>
				</view>
				<view class="gift-note">
					<view class="b-tit">备注信息</view>
					<view class="b-cont">
						<input placeholder="请输入其他要求" @input="onInputText" data-field="note" />
					</view>
				</view>
			</view>
			<view class="box">
				<view class="b-row f-fund">
					<view class="b-tit">商品总价:</view>
					<view class="b-cont">
						<view class="s-num">¥{{orderData.total_price}}</view>
					</view>
				</view>
				<view class="b-row f-fund">
					<view class="b-tit">优惠金额:</view>
					<view class="b-cont">
						<view class="s-num">¥{{orderData.total_discount}}</view>
					</view>
				</view>
				<view class="b-row f-fund">
					<view class="b-tit">应付金额:</view>
					<view class="b-cont">
						<view class="s-num">¥{{orderData.total_payment}}</view>
					</view>
				</view>
			</view>
			<view class="box">
				<view class="b-row f-wallet">
					<view class="b-tit">
						<view class="s-tit">储值卡</view>
						<text class="s-left">
							<text>(余额：¥{{orderData.fund_amount_left}}</text>
							<text class="s-use" v-if="fundAmount>0">-¥{{fundAmount}}</text>
							<text>)</text>
						</text>
						<view class="s-recharge" @click="onRecharge">去充值</view>
					</view>
					<view class="b-cont">
						<switch color="#DA5650" :disabled="orderData.fund_amount_left <=0" @change="onSwitchChange"
							data-field="use_fund_amount">
						</switch>
					</view>
				</view>
			</view>
			<view class="box" v-if="totalPayment > 0">
				<radio-group @change="onRadioChange" v-for="(item, index) in payList" :key="index">
					<view class="b-row f-pay">
						<view class="b-tit">
							<view class="iconfont" :class="'icon-'+item.icon"></view>
							<view class="s-tit">{{item.name}}</view>
						</view>
						<view class="b-cont">
							<radio value="wechat" :checked="item.checked" color="#DA5650" />
						</view>
					</view>
				</radio-group>
			</view>
			<view class="navbar">
				<view class="b-amount">
					<view class="s-tit">现付:</view>
					<view class="s-num">¥{{totalPayment}}</view>
				</view>
				<view class="b-action">
					<view class="s-exchange" @click="onSubmitOrder">确认下单</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" :top="300"></load-more>
		<!-- 首页 -->
		<app-home></app-home>
	</view>
</template>

<script>
	import {
		handleCart,
		previewOrder,
		submitOrder,
		oncePay
	} from '@/service/api/order.js'
	import {
		requestPayment,
		subscriberMessage
	} from '@/service/request/main.js'
	import Util from '@/common/util.js'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				disable: {
					submit: false
				},
				shopData: {},
				deskData: null,
				orderData: {},
				formData: {
					shop_id: 0,
					desk_id: 0,
					enjoy_type: '',
					contact: '',
					goods_list: [],
					note: '',
					total_price: 0,
					total_payment: 0,
				},
				deployList: [{
						icon: 'icon-meat',
						alias: 'meat',
						name: '店内用餐',
					},
					{
						icon: 'icon-pack',
						alias: 'pack',
						name: '打包外带'
					}
				],
				deployIndex: 0,
				payList: [{
					icon: 'pay-wechat',
					alias: 'wechat',
					name: '微信支付',
					checked: true
				}],
				payIndex: 0,
				hasLoad: false,
				loadingType: 1,
				loadingText: ''
			}
		},
		onLoad(options) {
			let goodsData = options.goods_data
			this.goodsData = JSON.parse(goodsData)
		},
		onShow() {
			this.loadPreview()
		},
		computed: {
			totalPayment() {
				let totalPayment = this.orderData.total_payment
				if (this.formData.use_fund_amount) {
					if (this.orderData.fund_amount_left - this.orderData.total_payment >= 0) {
						totalPayment = 0
					} else {
						totalPayment = this.orderData.total_payment - this.orderData.fund_amount_left
					}
				}
				return totalPayment
			},
			fundAmount() {
				return this.orderData.total_payment - this.totalPayment
			}
		},
		methods: {
			loadPreview() {
				previewOrder({
					goods_list: this.goodsData,
					shop_id: this.$store.getters.shopId,
					desk_id: this.$store.getters.deskId
				}).then(([err, res]) => {
					console.log('previewOrder', err, res)
					if (!err) {
						this.hasLoad = true
						this.loadingType = -1
						this.orderData = res
						this.shopData = res.shop_data
						this.deskData = res.desk_data
						this.formData.shop_id = res.shop_data.id
						this.formData.goods_list = res.goods_list
						this.formData.total_price = res.total_price
						this.formData.total_payment = res.total_payment
					} else {
						this.loadingType = 3
						this.loadingText = err.data.msg || '加载失败'
					}
				})
			},
			onChooseDeploy(index) {
				console.log('onChooseDeploy', index)
				this.deployIndex = index
			},
			onScanCode() {
				// #ifdef  MP-WEIXIN
				uni.scanCode().then(([err, res]) => {
					console.log('uni.scanCode', err, res)
					if (!err) {
						let urlData = Util.urlDecode(res.path)
						console.log('urlData', urlData)
						if (!urlData || !urlData.scene) {
							return this.$logic.showToast('点餐码不对')
						}
						let sceneData = null
						if (res.path.indexOf('&') !== -1) {
							sceneData = Util.urlDecode(res.path.slice(res.path.indexOf('?scene=') + 7))
						} else {
							sceneData = Util.urlDecode(decodeURIComponent(urlData.scene))
						}
						console.log('sceneData', sceneData)
						if (!sceneData || !sceneData.did) {
							return this.$logic.showToast('点餐码不对')
						}
						if (sceneData.sid != this.formData.shop_id) {
							return this.$logic.showToast('不是当前门店的点餐码')
						}
						this.deskData = {
							id: sceneData.did,
							number: sceneData.number,
						}
						this.formData.desk_id = this.deskData.id
						this.$store.commit('setDeskId', this.deskData.id)
					}
				})
				// #endif
				
				// #ifdef  H5
				const that = this
				wx.scanQRCode({
					needResult: 1,
					success: function(res) {
						console.log('wx.scanQRCode', res)
						let urlData = Util.urlDecode(res.resultStr)
						console.log('urlData', urlData)
						if (!urlData || !urlData.scene) {
							return that.$logic.showToast('点餐码不对')
						}
						let sceneData = null
						if (res.resultStr.indexOf('&') !== -1) {
							sceneData = Util.urlDecode(res.resultStr.slice(res.resultStr.indexOf('?scene=') + 7))
						} else {
							sceneData = Util.urlDecode(decodeURIComponent(urlData.scene))
						}
						console.log('sceneData', sceneData)
						if (!sceneData || !sceneData.did) {
							return that.$logic.showToast('点餐码不对')
						}
						if (sceneData.sid != that.formData.shop_id) {
							return that.$logic.showToast('不是当前门店的点餐码')
						}
						that.deskData = {
							id: sceneData.did,
							number: sceneData.number,
						}
						that.formData.desk_id = that.deskData.id
						that.$store.commit('setDeskId', that.deskData.id)
					}
				})
				// #endif
			},
			// 输入内容
			onInputText(e) {
				console.log('onInputText', e)
				this.formData[e.target.dataset.field] = e.detail.value
			},
			onSwitchChange(e) {
				console.log('onSwitchChange', e)
				this.formData[e.target.dataset.field] = e.detail.value ? 1 : 0
			},
			onRadioChange(e) {
				console.log('onRadioChange', e)
			},
			onSubmitOrder() {
				if (this.disable.submit) {
					return
				}
				this.formData.enjoy_type = this.deployList[this.deployIndex].alias
				if (this.formData.enjoy_type === 'meat') {
					if (!this.deskData) {
						return this.$logic.showToast('请扫点餐码')
					}
					this.formData.desk_id = this.deskData.id
				}
				// #ifdef  MP-WEIXIN
				if (this.orderData.subscribe_template_ids.length > 0) {
					subscriberMessage(this.orderData.subscribe_template_ids).then(([err, res]) => {
						console.log('subscriberMessage', err, res)
						this.execSubmitOrder()
					})
				} else {
					this.execSubmitOrder()
				}
				// #endif
				// #ifdef  H5
				this.execSubmitOrder()
				// #endif
			},
			execSubmitOrder() {
				this.disable.submit = true
				submitOrder(this.formData).then(([err, res]) => {
					console.log('submitOrder', err, res)
					this.disable.submit = false
					if (!err) {
						for (let i = 0; i < this.goodsData.length; i++) {
							let goodsData = this.goodsData[i]
							if (goodsData.cart_id) {
								handleCart(goodsData.cart_id, 'delete').then(([err, res]) => {
									if (!err) {
										this.$store.commit('delCartItem', goodsData.cart_id)
									}
								})
							}
						}
						if (res.payment_status == 1) {
							const payAlias = this.payList[this.payIndex].alias
							requestPayment(res.order_no, payAlias, 'onceOrder')
						} else {
							uni.reLaunch({
								url: '/pages/order/detail?order_no=' + res.order_no
							})
						}
					}
				})
			},
			onRecharge() {
				uni.navigateTo({
					url: '/pages/user/fund'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	/* input 样式 */
	.input-placeholder,
	.placeholder {
		color: #999999;
		text-align: right;
	}

	.wrap {
		padding-bottom: 150upx;
	}

	.eat-method {
		margin-top: 30upx;
		margin-bottom: -30upx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.b-title {
			flex: 1;
			height: 100upx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #eee;

			&.f-active {
				background: #fff;
			}

			.iconfont {
				font-size: 40upx;
				color: #666;
			}

			.s-tit {
				margin-left: 20upx;
				font-size: 30upx;
				color: #999;
			}
		}
	}


	.gift-head {
		margin-top: 30upx;
		padding: 20upx 30upx;
		font-size: 28upx;
		color: #333;
		background: #fff;
		border-bottom: 2upx solid #eee;
	}

	.gift-list {
		background: #fff;

		.gift-item {
			padding: 30upx;
			border-bottom: 2upx solid #eee;
			display: flex;
			align-items: center;

			.b-left {
				width: 160upx;
				height: 160upx;

				image {
					width: 160upx;
					height: 160upx;
				}
			}

			.b-main {
				margin-left: 30upx;
				flex: 1;
				display: flex;
				flex-direction: column;

				.b-title {
					font-size: 28upx;
					color: #333;
				}


				.b-spec {
					margin-top: 20upx;
					display: flex;
					align-items: center;
					height: 30upx;

					.s-tit {
						margin-right: 10upx;
						font-size: 24upx;
						color: #999;
					}

					.s-value {
						font-size: 24upx;
						color: #666;
					}
				}

				.b-fund {
					margin-top: 20upx;
					display: flex;
					align-items: baseline;

					.s-count {
						font-size: 26upx;
						color: #666;
					}

					.s-worth {
						margin-left: 20upx;
						font-size: 26upx;
						color: #999;
					}

					.s-num {
						margin-left: 30upx;
						font-size: 32upx;
						color: #DA5650;
					}
				}
			}
		}

		.gift-note {
			padding: 20upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.b-tit {
				width: 240upx;
				font-size: 28upx;
				color: #333;
			}

			.b-cont {
				width: 400upx;
				font-size: 24upx;
				color: #333;

				input {
					font-size: 28upx;
					text-align: right;
				}

				.input-placeholder,
				.placeholder {
					font-size: 28upx;
				}
			}
		}
	}

	.box {
		margin-top: 30upx;
		padding: 30upx;
		background: #fff;

		.b-row {
			margin-bottom: 30upx;
			display: flex;
			justify-content: space-between;

			&:last-child {
				margin-bottom: 0;
			}

			&.f-fund {

				.b-cont {
					.s-num {
						font-size: 32upx;
						color: #DA5650;
					}
				}
			}

			&.f-wallet {
				align-items: center;

				.b-tit {
					width: 560upx;
					display: flex;
					align-items: center;

					.s-tit {
						margin-right: 10upx;
						color: #666;
					}

					.s-left {
						.s-use {
							margin-left: 10upx;
							color: #DA5650;
						}
					}

					.s-recharge {
						margin-left: 30upx;
						color: #EC9C4E;
					}
				}

				.b-cont {
					switch {
						transform: scale(0.6)
					}
				}
			}

			&.f-pay {
				.b-tit {
					width: 320upx;
					display: flex;
					align-items: center;

					.iconfont {
						font-size: 50upx;
						color: #6CB37A;
					}

					.s-tit {
						margin-left: 10upx;
						font-size: 28upx;
					}
				}

				.b-cont {
					radio {
						transform: scale(0.8)
					}
				}
			}

			&.f-shop {
				.b-cont {
					text-align: right;
					font-size: 28upx;
					height: 50upx;

					&.f-scan {
						color: #999;

						.iconfont {
							margin-right: 10upx;
						}
					}

					&.f-desk {}

					&.f-phone {
						input {
							font-size: 28upx;
							color: #333;
						}

						.input-placeholder,
						.placeholder {
							font-size: 28upx;
						}
					}
				}
			}

			.b-tit {
				font-size: 28upx;
				width: 160upx;
			}

			.b-cont {
				// flex: 1;
				display: flex;
				align-items: center;
			}

			.s-text {
				font-size: 28upx;
				color: #333;
			}
		}
	}

	.navbar {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 100upx;
		background: #fff;
		border-top: 1upx solid #f6f6f6;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.b-amount {
			margin-left: 30upx;
			display: flex;
			align-items: center;

			.s-tit {
				font-size: 28upx;
				color: #333;
			}

			.s-num {
				margin-left: 10upx;
				font-size: 32upx;
				color: #DA5650;
			}
		}

		.b-action {
			.s-exchange {
				background: #DA5650;
				height: 100upx;
				padding: 0 60upx;
				line-height: 100upx;
				font-size: 30upx;
				color: #fff;
				letter-spacing: 4upx;
			}
		}
	}
</style>
